<?php

include 'DBconn.php';

class CurrencyMaster {

    public function addNewCurrency($VObject) {
        $query = "INSERT INTO currency_header(country_serialnumber_fk,currency_name,currency_symbol,currency_code,cre_userid,cre_datetime,activestatus) values('$VObject->country_serialnumber_fk','$VObject->currency_name','$VObject->currency_symbol','$VObject->currency_code','$VObject->cre_userid','$VObject->cre_datetime',1)";
        $s = mysql_query($query) or die(mysql_error());
        if ($s == 1) {
            $last_id = mysql_insert_id();
            $query1 = "insert into currency_detail(to_currency_fk,currency_value,currency_todate,cre_userid,cre_datetime,activestatus) values ('$last_id',1,'$VObject->cre_datetime','$VObject->cre_userid','$VObject->cre_datetime',1)";
            $s1 = mysql_query($query1) or die(mysql_error());
        }
    }

    public function getAllCurrency() {
        $result = mysql_query("SELECT cur.serialnumber,cur.country_serialnumber_fk,cur.currency_name,cur.currency_symbol,cur.currency_code,cur.cre_userid,cur.cre_datetime,cur.activestatus,con.country_name
		FROM currency_header as cur LEFT JOIN country_header as con ON con.activestatus=1 and con.serialnumber=cur.country_serialnumber_fk WHERE cur.activestatus ='1'") or die(mysql_error());
        $ret_arr = array();
        while ($row = mysql_fetch_object($result)) {
            $temp = new VoCurrencyHeader();
            $temp->serialnumber = (int) $row->serialnumber;
            $temp->currency_name = $row->currency_name;
			$temp->country_name = $row->country_name;
            $temp->country_serialnumber_fk = (int)$row->country_serialnumber_fk;
            $temp->currency_symbol = $row->currency_symbol;
            $temp->currency_code = $row->currency_code;
            $temp->cre_userid = (int) $row->cre_userid;
            $temp->cre_datetime = $row->cre_datetime;
            $temp->activestatus = (boolean) $row->activestatus;
			
			$result1 = mysql_query("SELECT currency_value from currency_detail WHERE activestatus ='1'
					   and to_currency_fk='$row->serialnumber' ORDER BY detserialnumber DESC LIMIT 1") or die(mysql_error());
			$row1 = mysql_fetch_row($result1);
			$temp->currency_value =(double)$row1[0];
			
            $ret_arr[] = $temp;
        }
        mysql_free_result($result);
        return $ret_arr;
    }

    public function addCurrencyDetails($VObject) {
        $query = "insert into currency_detail(to_currency_fk,currency_value,currency_todate,cre_userid,cre_datetime,activestatus) values ('$VObject->to_currency_fk','$VObject->currency_value','$VObject->currency_todate','$VObject->cre_userid','$VObject->cre_datetime',1)";
        $s = mysql_query($query) or die(mysql_error());
    }

    public function getAllCurrencyDetails() 
	{
        $ret = mysql_query("SELECT serialnumber,currency_name from currency_header WHERE activestatus ='1'") or die(mysql_error());
        $ret_array = array();
        while ($row = mysql_fetch_object($ret)) 
		{
            $result = mysql_query("select * from currency_detail where to_currency_fk='$row->serialnumber' and activestatus=1 ORDER BY detserialnumber DESC LIMIT 1") or die(mysql_error());
            $row1 = mysql_fetch_object($result);
            if (!empty($row1)) {
                $temp = new VoCurrencyDetail();
                $temp->detserialnumber = (int) $row1->detserialnumber;
                $temp->to_currency_fk = (int) $row1->to_currency_fk;
				$temp->currency_name = $row->currency_name;
                $temp->currency_value = (double)$row1->currency_value;
				$temp->currency_todate = date("d-m-Y", strtotime($row1->currency_todate));
                $temp->cre_userid = (int) $row1->cre_userid;
                $temp->cre_datetime = $row1->cre_datetime;
                $temp->activestatus = $row1->activestatus;
                $ret_array[] = $temp;
            }
        }
        return $ret_array;
    }

    public function getAllCurrencyDetSerials() {
        $result = mysql_query("select * from currency_detail where  activestatus=1") or die(mysql_error());
        $ret_array = array();
        while ($row1 = mysql_fetch_object($result)) {
            $temp = new VoCurrencyDetSerial();
            $temp->detserialnumber = (int) $row1->detserialnumber;
            $temp->currency_value = (double) $row1->currency_value;
            $temp->to_currency_fk = (int) $row1->to_currency_fk;
            $ret_array[] = $temp;
        }
        mysql_free_result($result);
        return $ret_array;
    }

    public function editCurrencyHeader($VObject) {
        $query = "update currency_header set currency_name='$VObject->currency_name' where serialnumber='$VObject->serialnumber' and activestatus=1";
        $s = mysql_query($query) or die(mysql_error());
    }

    public function deleteCurrency($serialnumber) {
        $s = mysql_query("update currency_header set activestatus=0 where serialnumber='$serialnumber'") or die(mysql_error());
        return $serialnumber;
    }
	
	public function getCountriesNotHaveCurrency() {
        $result = mysql_query("select * from country_header where serialnumber Not In (select country_serialnumber_fk From currency_header where activestatus=1) and activestatus=1") or die(mysql_error());
        $res_arr = array();
        while ($row = mysql_fetch_object($result)) {
            $tmp = new VOCountryMaster();
            $tmp->serialnumber = (int) $row->serialnumber;
            $tmp->country_name = $row->country_name;
            $tmp->cre_userid_fk = (int) $row->cre_userid_fk;
            $tmp->cre_datetime = $row->cre_datetime;
            $tmp->activestatus = (boolean) $row->activestatus;
            $res_arr[] = $tmp;
        }
        mysql_free_result($result);
        return $res_arr;
    }
}

class VOCountryMaster {
    var $serialnumber;
    var $country_name;
    var $cre_userid_fk;
    var $cre_datetime;
    var $activestatus;
}

class VoCurrencyHeader {

    var $serialnumber;
    var $country_serialnumber_fk;
	var $country_name;
    var $currency_name;
    var $currency_symbol;
    var $currency_code;
    var $cre_userid;
    var $cre_datetime;
    var $activestatus;
	var $currency_value;

}

class VoCurrencyDetail {

    var $detserialnumber;
    var $to_currency_fk;
	var $currency_name;
    var $currency_value;
    var $currency_todate;
    var $cre_userid;
    var $cre_datetime;
    var $activestatus;

}

class VoCurrencyDetSerial {

    var $detserialnumber;
    var $currency_value;
    var $to_currency_fk;

}

?>